<template>
    <div class="app-container">

      <div class="filter-container">
			  <el-button @click="addPermission(0,0)" type="primary" class="filter-item">添加</el-button>
        <el-button @click="getRecords" type="danger" class="filter-item">刷新</el-button>
		  </div>


      <el-table
        v-loading="listLoading"
    :data="list"
    style="width: 100%;margin-bottom: 20px;"
    row-key="id"
    border
    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
    <el-table-column prop="id" label="ID" width="80"></el-table-column>
    <el-table-column prop="path" label="path" width="100"></el-table-column>
    <el-table-column prop="name" label="name" width="100"></el-table-column>
    <el-table-column prop="component" label="component" width="200"></el-table-column>
    <el-table-column prop="title" label="标题" width="100"></el-table-column>
    <el-table-column prop="icon" label="图标" width="150"></el-table-column>
    <el-table-column prop="pid" label="上级ID" width="100"></el-table-column>
    <el-table-column prop="sort" label="排序" width="50"></el-table-column>
    <el-table-column width="160" prop="created_at" label="创建时间"></el-table-column>

    <el-table-column fixed="right" label="操作" width="150">
      <template slot-scope="scope">
        <el-button size="mini" type="text" @click="addPermission(scope.$index, scope.row.id)">添加</el-button>
        <el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
		</el-table-column>
  </el-table>

  <el-dialog :title="isAdd?'添加权限':'编辑权限'" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="path" prop="path">
          <el-input v-model="form.path"></el-input>
        </el-form-item>
        <el-form-item label="name" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="component" prop="component">
          <el-input v-model="form.component"></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="显示/隐藏">
          <el-radio v-model="form.is_show" label="1">显示</el-radio>
          <el-radio v-model="form.is_show" label="2">隐藏</el-radio>
        </el-form-item>
        <el-form-item v-if="isAdd">
          <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
          <el-button>取消</el-button>
        </el-form-item>
        <el-form-item v-else>
          <el-button type="primary" @click="onSubmit('form')">修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    </div>
</template>

<script>

import { myGet, myPost } from '@/utils/myRequest'

const urlPermissionLst = '/guard/rbac/permission/lst'
const urlPermissionAdd = '/guard/rbac/permission/add'
const urlPermissionDel = '/guard/rbac/permission/del'
const urlPermissionGet = '/guard/rbac/permission/getPermission'

export default {
    data() {
      return {
        list:[],
        isAdd:true,
        listLoading:true,
        dialogFormVisible:false,
        form:{
          id:0,
          name:'',
          path:'',
          component:'',
          title:'',
          icon:'table',
          sort:0,
          pid:0,
          is_show:'1'
        },
        rules: {
          name: [
            { required: true, message: '必填', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '必填', trigger: 'blur' }
          ],
          component: [
            { required: true, message: '必填', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '必填', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '必填', trigger: 'blur' },
            { type: 'number', message: '必须是数字', trigger: 'blur' }
          ],
        },
      }
    },
    methods: {
      getRecords(){
        this.listLoading = true
        myGet(urlPermissionLst).then(res => {
          this.listLoading = false
          if(res.status){
            this.list = res.data.records
          }
        })
      },
      addPermission(index,id){
        this.isAdd = true
        this.dialogFormVisible = true
        Object.assign(this.form, this.$options.data().form)
        this.$set(this.form,'pid',id)
      },
      onSubmit(formName){
        this.$refs[formName].validate((valid)=>{
          if(valid){
            myPost(urlPermissionAdd,this.form).then(res => {
              this.dialogFormVisible = false
              if(res.status){
                this.$message.success(res.msg)
                this.getRecords()
              }
            })
          }
        })
      },
      handleEdit(index,row){
        const id = row.id
        myGet(urlPermissionGet,{id}).then(res => {
          if(res.status){
            const info = res.data.records[0]
            info.is_show = info.is_show+''
            Object.assign(this.form,info)
            this.isAdd = false
            this.dialogFormVisible = true

          }
        })
      },

      handleDelete(index,row){
        this.$confirm('确定要删除吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const id = row.id
            myGet(urlPermissionDel,{id}).then(res => {
              if(res.status){
                this.$message.success(res.msg)
                this.getRecords()
              }
            })
          });
        }
    },

    mounted() {
      this.getRecords()
    },

}
</script>

<style>

</style>
